package com.yan.demo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * @author taoge
 * @Date 2024/8/6 14:08
 * @Description 字节数组（内存流）
 */
public class Demo3 {
    public static void main(String[] args) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bos.write("abc".getBytes());
        bos.write("abc".getBytes());
        bos.write("abc".getBytes());
        bos.write("abc".getBytes());
        bos.flush();
        System.out.println(Arrays.toString(bos.toByteArray()));
        bos.close();

        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        int len = -1;
        byte[] car = new byte[1024];
        while((len = bis.read(car)) != -1 ){
            System.out.println(new String(car , 0 , len));
        }
    }
}
